package com.example.statemachine.machine.base;

import com.example.statemachine.machine.enums.EnumFsmEvent;
import org.springframework.core.NamedThreadLocal;

/**
 * @author jianwei.wang
 * @version v1.0
 * @date 2019/5/8 17:32
 * 状态体基类
 **/
public class FsmState<T> {

    private ThreadLocal<T> threadLocal = new NamedThreadLocal("credential");


    public FsmResBody tempStorage() {
        return FsmResBody.notTempStorage();
    }

    public FsmResBody submit() {
        return FsmResBody.notSubmit();
    }

    public FsmResBody withDraw() {
        return FsmResBody.notWithDraw();
    }

    public FsmResBody review() {
        return FsmResBody.notReview();
    }

    public FsmResBody antiReview() {
        return FsmResBody.notAntiReview();
    }

    public FsmResBody cancel() {
        return FsmResBody.notCancel();
    }

    public FsmResBody execute(EnumFsmEvent event) {
        switch (event) {
            case TEMP_STORAGE:
                return this.tempStorage();
            case SUBMIT:
                return this.submit();
            case WITH_DRAW:
                return this.withDraw();
            case REVIEW:
                return this.review();
            case ANTI_REVIEW:
                return this.antiReview();
            case CANCEL:
                return this.cancel();
            default:
                return FsmResBody.notEvent();
        }
    }

    public T getT() {
        T t = threadLocal.get();
        threadLocal.remove();
        return t;
    }

    public void setT(T t) {
        threadLocal.set(t);
    }
}
